﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;

namespace KorTransLib
{
    public class RuhsatKoordinat
    {
        // <Koordinat PaftaNo="1" SıraNo="2" Y="559050" X="4217550" />
        public int PaftaNo { get; set; }
        public int SıraNo { get; set; }
        public int Sağa { get; set; }
        public int Yukarı { get; set; }
        public RuhsatKoordinat(int PaftaNo, int SıraNo, int Sağa, int Yukarı)
        {
            this.PaftaNo = PaftaNo;
            this.SıraNo = SıraNo;
            this.Sağa = Sağa;
            this.Yukarı = Yukarı;
        }
    }

    /// <summary>
    /// Maden İşletme Ruhsatı ve benzeri ruhsatları UTM koordinatları ile birlikte temsil eder.
    /// </summary>
    public class Ruhsat
    {
        public string İl { get; set; }
        public string İlçe { get; set; }
        public string Köy { get; set; }
        public int Numara { get; set; }
        public string Grup { get; set; }
        public DateTime YürürlükBaşlangıç { get; set; }
        public DateTime YürürlükBitiş { get; set; }
        public int ErişimNumarası { get; set; }
        public string Alan { get; set; }
        public string Safha { get; set; }
        public string Sahibi { get; set; }
        public string TCKimlikNo { get; set; }
        public string VergiDairesi { get; set; }
        public Int64 VergiNumarası { get; set; }
        public string Adres { get; set; }
        public List<string> Paftalar;
        public List<RuhsatKoordinat> Koordinatlar;

        public Ruhsat()
        {
            Paftalar = new List<string>();
            Koordinatlar = new List<RuhsatKoordinat>();
        }

        public override string ToString()
        {
            return string.Format("{0} {1} {2} {3}-{4}", İl, İlçe, Köy, Numara, ErişimNumarası);
        }

        public void Load(string filename)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(filename);
            XPathNavigator nav = xmlDocument.CreateNavigator();
            XPathNodeIterator iterator = nav.Select("/Ruhsat");
            while (iterator.MoveNext())
            {
                iterator.Current.MoveToChild("İl", string.Empty);
                İl = iterator.Current.Value;
                iterator.Current.MoveToParent();

                iterator.Current.MoveToChild("İlçe", string.Empty);
                İlçe = iterator.Current.Value;
                iterator.Current.MoveToParent();

                iterator.Current.MoveToChild("Köy", string.Empty);
                Köy = iterator.Current.Value;
                iterator.Current.MoveToParent();

                iterator.Current.MoveToChild("Numara", string.Empty);
                Numara = int.Parse(iterator.Current.Value);
                iterator.Current.MoveToParent();

                iterator.Current.MoveToChild("Grup", string.Empty);
                Grup = iterator.Current.Value;
                iterator.Current.MoveToParent();

                iterator.Current.MoveToChild("Yürürlük", string.Empty);

                iterator.Current.MoveToChild("Başlangıç", string.Empty);
                YürürlükBaşlangıç = DateTime.ParseExact(iterator.Current.Value, "dd.MM.yyyy", null);
                iterator.Current.MoveToParent();

                iterator.Current.MoveToChild("Bitiş", string.Empty);
                YürürlükBitiş = DateTime.ParseExact(iterator.Current.Value, "dd.MM.yyyy", null);
                iterator.Current.MoveToParent();

                iterator.Current.MoveToParent();

                iterator.Current.MoveToChild("ErişimNumarası", string.Empty);
                ErişimNumarası = int.Parse(iterator.Current.Value);
                iterator.Current.MoveToParent();

                iterator.Current.MoveToChild("Alan", string.Empty);
                Alan = iterator.Current.Value;
                iterator.Current.MoveToParent();

                iterator.Current.MoveToChild("Safha", string.Empty);
                Safha = iterator.Current.Value;
                iterator.Current.MoveToParent();

                iterator.Current.MoveToChild("Sahibi", string.Empty);
                Sahibi = iterator.Current.Value;
                iterator.Current.MoveToParent();

                iterator.Current.MoveToChild("TCKimlikNo", string.Empty);
                TCKimlikNo = iterator.Current.Value;
                iterator.Current.MoveToParent();

                iterator.Current.MoveToChild("Vergi", string.Empty);

                iterator.Current.MoveToChild("Daire", string.Empty);
                VergiDairesi = iterator.Current.Value;
                iterator.Current.MoveToParent();

                iterator.Current.MoveToChild("Numara", string.Empty);
                VergiNumarası = Int64.Parse(iterator.Current.Value);
                iterator.Current.MoveToParent();

                iterator.Current.MoveToParent();

                iterator.Current.MoveToChild("Adres", string.Empty);
                Adres = iterator.Current.Value;
                iterator.Current.MoveToParent();

                iterator.Current.MoveToChild("Paftalar", string.Empty);
                Paftalar.Clear();

                iterator.Current.MoveToChild("Pafta", string.Empty);
                do
                {
                    Paftalar.Add(iterator.Current.Value);
                }
                while (iterator.Current.MoveToNext());
                iterator.Current.MoveToParent();

                iterator.Current.MoveToParent();



                iterator.Current.MoveToChild("Koordinatlar", string.Empty);
                Koordinatlar.Clear();

                // <Koordinat PaftaNo="1" SıraNo="2" Y="559050" X="4217550" />
                iterator.Current.MoveToChild("Koordinat", string.Empty);
                do
                {
                    Koordinatlar.Add(new RuhsatKoordinat(
                        int.Parse(iterator.Current.GetAttribute("PaftaNo", "")),
                        int.Parse(iterator.Current.GetAttribute("SıraNo", "")),
                        int.Parse(iterator.Current.GetAttribute("Y", "")),
                        int.Parse(iterator.Current.GetAttribute("X", ""))));
                }
                while (iterator.Current.MoveToNext());
                iterator.Current.MoveToParent();
            }
        }

        public XmlDocument GetXML()
        {
            XmlDocument xmlDoc = new XmlDocument();
            // Write down the XML declaration
            XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "iso-8859-9", null);

            // Create the root element
            XmlElement rootNode = xmlDoc.CreateElement("Ruhsat");
            xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
            xmlDoc.AppendChild(rootNode);

            // Create a new <Category> element and add it to the root node
            XmlElement xe;

            xe = xmlDoc.CreateElement("İl");
            xe.AppendChild(xmlDoc.CreateTextNode(İl));
            rootNode.AppendChild(xe);

            xe = xmlDoc.CreateElement("İlçe");
            xe.AppendChild(xmlDoc.CreateTextNode(İlçe));
            rootNode.AppendChild(xe);

            xe = xmlDoc.CreateElement("Köy");
            xe.AppendChild(xmlDoc.CreateTextNode(Köy));
            rootNode.AppendChild(xe);

            xe = xmlDoc.CreateElement("Numara");
            xe.AppendChild(xmlDoc.CreateTextNode(Numara.ToString()));
            rootNode.AppendChild(xe);

            xe = xmlDoc.CreateElement("Grup");
            xe.AppendChild(xmlDoc.CreateTextNode(Grup));
            rootNode.AppendChild(xe);

            XmlElement xec;
            xe = xmlDoc.CreateElement("Yürürlük");
            xec = xmlDoc.CreateElement("Başlangıç");
            xec.AppendChild(xmlDoc.CreateTextNode(YürürlükBaşlangıç.ToString("dd.MM.yyyy")));
            xe.AppendChild(xec);
            xec = xmlDoc.CreateElement("Bitiş");
            xec.AppendChild(xmlDoc.CreateTextNode(YürürlükBitiş.ToString("dd.MM.yyyy")));
            xe.AppendChild(xec);

            rootNode.AppendChild(xe);

            xe = xmlDoc.CreateElement("ErişimNumarası");
            xe.AppendChild(xmlDoc.CreateTextNode(ErişimNumarası.ToString()));
            rootNode.AppendChild(xe);

            xe = xmlDoc.CreateElement("Alan");
            xe.AppendChild(xmlDoc.CreateTextNode(Alan));
            rootNode.AppendChild(xe);

            xe = xmlDoc.CreateElement("Safha");
            xe.AppendChild(xmlDoc.CreateTextNode(Safha));
            rootNode.AppendChild(xe);

            xe = xmlDoc.CreateElement("Sahibi");
            xe.AppendChild(xmlDoc.CreateTextNode(Sahibi));
            rootNode.AppendChild(xe);

            xe = xmlDoc.CreateElement("TCKimlikNo");
            xe.AppendChild(xmlDoc.CreateTextNode(TCKimlikNo));
            rootNode.AppendChild(xe);

            xe = xmlDoc.CreateElement("Vergi");
            xec = xmlDoc.CreateElement("Daire");
            xec.AppendChild(xmlDoc.CreateTextNode(VergiDairesi));
            xe.AppendChild(xec);
            xec = xmlDoc.CreateElement("Numara");
            xec.AppendChild(xmlDoc.CreateTextNode(VergiNumarası.ToString()));
            xe.AppendChild(xec);

            rootNode.AppendChild(xe);

            xe = xmlDoc.CreateElement("Adres");
            xe.AppendChild(xmlDoc.CreateTextNode(Adres));
            rootNode.AppendChild(xe);


            xe = xmlDoc.CreateElement("Paftalar");
            foreach (string p in Paftalar)
            {
                xec = xmlDoc.CreateElement("Pafta");
                xec.AppendChild(xmlDoc.CreateTextNode(p));
                xe.AppendChild(xec);
            }
            xe.AppendChild(xec);
            rootNode.AppendChild(xe);

            xe = xmlDoc.CreateElement("Koordinatlar");
            foreach (RuhsatKoordinat rk in Koordinatlar)
            {
                xec = xmlDoc.CreateElement("Koordinat");
                xec.SetAttribute("PaftaNo", rk.PaftaNo.ToString());
                xec.SetAttribute("SıraNo", rk.SıraNo.ToString());
                xec.SetAttribute("Y", rk.Sağa.ToString());
                xec.SetAttribute("X", rk.Yukarı.ToString());
                xe.AppendChild(xec);
            }
            xe.AppendChild(xec);
            rootNode.AppendChild(xe);

            return xmlDoc;
        }

        public string GetXMLString()
        {
            XmlDocument xmlDoc = GetXML();
            StringBuilder sbRet = new StringBuilder();
            using (System.IO.StringWriter sw = new System.IO.StringWriter(sbRet))
                xmlDoc.Save(sw);
            return sbRet.ToString();            
        }

        public void Save(string filename)
        {
            GetXML().Save(filename);
        }
        /*
<?xml version="1.0" encoding="UTF-8"?>
<Ruhsat>
	<İl></İl>
	<İlçe></İlçe>
	<Köy></Köy>
	<Numara></Numara>
	<Grup></Grup>
	<Yürürlük>
		<Başlangıç></Başlangıç>
		<Bitiş></Bitiş>
	</Yürürlük>
	<ErişimNumarası></ErişimNumarası>
	<Alan>xy ha</Alan>
	<Safha></Safha>
	<Sahibi>XYZ</Sahibi>
	<TCKimlikNo></TCKimlikNo>
	<Vergi>
		<Daire></Daire>
		<Numara></Numara>
	</Vergi>
	<Adres></Adres>
	<Paftalar>
		<Pafta></Pafta>
	</Paftalar>
	<Koordinatlar>
		<Koordinat PaftaNo="1" SıraNo="1" Y="*" X="*" />
		<Koordinat PaftaNo="1" SıraNo="2" Y="*" X="*" />
	</Koordinatlar>
</Ruhsat>

         * */
    }
}
